package connect3.team7.util;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.RemoteException;

import connect3.team7.*;

public class ClientController {
  private static ClientController instance;
  private String host_lookup;
  private Registry registry;
  private Host remoteObj;

/*
  private ClientController(String host_ip) {
    super();
    host_lookup = "HostController";
    retrieveRemoteHost(host_ip);
  }
*/

  private ClientController() {
    super();
    host_lookup = "HostController";
  }

  public static ClientController getInstance() {
    if (instance == null)
      instance = new ClientController();

    return instance;
  }

  private void retrieveRemoteHost(String host_ip) {
    try {
      registry = LocateRegistry.getRegistry(host_ip);
      remoteObj = (Host) registry.lookup(host_lookup);
    } catch(Exception e) {
      System.err.println("ClientController exception: ");
      e.printStackTrace(); 
    }
  }

  public void showAvailableHosts() {
    DBController db = DBController.getInstance();
    LinkedList<DBController.HostListing> l = db.getHostListing(); 

    ListIterator i = l.listIterator();

    while(i.hasNext()) {
      DBController.HostListing curr = i.next();

      System.out.println(curr.host_handle + " - " + curr.host_ip + " - " + curr.host_port + " - " + curr.player_slots);
    }
  }

  public void join(Player p) throws RemoteException {
    boolean ret = remoteObj.join(p);

    if (ret)
      System.out.println("joined game!");
    else
      System.out.println("booted!");
  }
}
